package com.dd.consul_consumer_feign.controller;

import java.net.URI;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

import com.dd.consul_consumer_feign.service.FeignService;


@RestController
public class AppController {

    @Autowired
    private FeignService feignService;
    
    @Autowired
    private DiscoveryClient discoveryClient;


    @RequestMapping("/")
    public String hello() {
        String result = feignService.hello();
        return result;
 //   	return "hello";
    }
    
    @RequestMapping("/services")
    public List<String> services() {
    	List<String> list = discoveryClient.getServices();
    	return list;
//        List<ServiceInstance> list = discoveryClient.getInstances("STORES");
//        if (list != null && list.size() > 0 ) {
//            return list.get(0).getUri().toString();
//        }
//        return null;
    }
    
    @RequestMapping("/services2")
    public URI services2() {
        List<ServiceInstance> list = discoveryClient.getInstances("consul-provider");
        if (list != null && list.size() > 0 ) {
            return list.get(0).getUri();
        }
        return null;
    }

}
